package com.glre.humandroid;

import java.util.Random;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Path;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.ArcShape;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.PathShape;
import android.graphics.drawable.shapes.RectShape;
import android.util.AttributeSet;
import android.view.View;

/**
 * Dessine sur le canvas et transforme le canvas en bitmap
 *
 */
public class CustomDrawableView extends View {
	private ShapeDrawable mDrawable;
	Bitmap b;
	int x = 100;
	int y = 10;
	int width = 100;
	int height = 150;
	int widthC = 0;
	int heightC = 0;

	public CustomDrawableView(final Context context, final AttributeSet attr) {
		super(context, attr);
		setDrawingCacheEnabled(true);
		Random randInt = new Random();
		int b = randInt.nextInt(3);
		int cercle = randInt.nextInt(10);
		int pourPath1;
		int pourPath2;

		Path path = new Path();
		path.moveTo(0, 0);
		pourPath1 = randInt.nextInt(350);
		pourPath2 = randInt.nextInt(350);
		path.lineTo(pourPath1, pourPath2);
		pourPath1 = randInt.nextInt(350);
		pourPath2 = randInt.nextInt(350);
		path.lineTo(pourPath1, pourPath2);
		pourPath1 = randInt.nextInt(350);
		pourPath2 = randInt.nextInt(350);
		path.lineTo(pourPath1, pourPath2);
		pourPath1 = randInt.nextInt(350);
		pourPath2 = randInt.nextInt(350);
		path.lineTo(pourPath1, pourPath2);
		path.close();

		switch (b) {
		case 1:
			mDrawable = new ShapeDrawable(new ArcShape(30, cercle * 100));
			break;
		case 2:
			mDrawable = new ShapeDrawable(new PathShape(path, cercle * 10,
					cercle * 10));
			break;
		case 3:
			mDrawable = new ShapeDrawable(new OvalShape());
			break;
		default:
			mDrawable = new ShapeDrawable(new RectShape());
			break;

		}
		mDrawable.setBounds(x, y, x + width, y + height);
		mDrawable.getPaint().setColor(Color.BLACK);
	}

	@Override
	protected void onDraw(final Canvas canvas) {

		widthC = canvas.getWidth();
		heightC = canvas.getHeight();
		b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
		Canvas c = new Canvas(b);

		c.drawColor(Color.WHITE);
		canvas.drawColor(Color.WHITE);
		mDrawable.draw(canvas);
		mDrawable.draw(c);


	}
}
